package edu.gatech.cs2340.group7.wheresmystuffapp.presenters;

import android.content.Intent;
import android.widget.Toast;
import edu.gatech.cs2340.group7.wheresmystuffapp.model.Account;
import edu.gatech.cs2340.group7.wheresmystuffapp.model.DataManager;
import edu.gatech.cs2340.group7.wheresmystuffapp.model.DatabaseHelper;
import edu.gatech.cs2340.group7.wheresmystuffapp.model.User;
import edu.gatech.cs2340.group7.wheresmystuffapp.views.CreateAccountView;
import edu.gatech.cs2340.group7.wheresmystuffapp.views.LoginView;

/**
 * This creates the logic for the Account Creation process
 * @author Magic 7
 *
 */
public class CreateAccountPresenter {
	private CreateAccountView myView;
	DatabaseHelper helper;

	public CreateAccountPresenter(CreateAccountView view) {
		myView = view;
		helper = new DatabaseHelper(view);
	}

	/**
	 * Creates a new account
	 * 
	 * @param email
	 *            the username of the account
	 * @param password
	 *            the password of the account
	 * @param confirmPassword
	 *            - should be same as password
	 * @param phoneNumber
	 *            the phone number or the account
	 * @return true if the account was created
	 */
	public boolean createAccount(String email, String password,
			String confirmPassword, String phoneNumber) {
		if (checkInputs(email, password, confirmPassword, phoneNumber)) {
			Account account = new User(email, password, phoneNumber);
			helper.addAccount(account);
			//DataManager.addAccount(email, account);
			if (myView != null) {
				//myView.setCreateAccountResult("Account Created");
				Toast.makeText(myView, "Account Created", Toast.LENGTH_SHORT).show();
				toLogin();
			}

			return true;
		} else {
			if (myView != null) {
				//myView.setCreateAccountResult("Invalid Information");
				Toast.makeText(myView, "Invalid Information", Toast.LENGTH_SHORT).show();
			}
			return false;
		}
	} // close createAccount

	/**
	 * Checks to see if the user has input valid information. Also checks to see
	 * if the username already exists in the system
	 * 
	 * @param username
	 *            The username trying to be created
	 * @param password
	 *            The proposed password
	 * @param confPassword
	 *            The confirmation of the proposed password
	 * @param phoneNumber
	 *            The user's phone number
	 * @return True if all the information is valid and false otherwise
	 */
	private boolean checkInputs(String username, String password,
			String confPassword, String phoneNumber) {
		boolean out = true;

		if (username != null && password != null && confPassword != null
				&& phoneNumber != null) { // If all the inputs are non-null

			if (helper.accountExists(username)) { // If the username
														// already exists in the
														// database
				out = false;
			} else if (!password.equals(confPassword)) { // if the passwords
															// don't match
				out = false;
			} else if (phoneNumber.length() < 7) { // TODO If the phone number
													// is incorrectly given
				out = false;
			}
		} else {
			out = false;
		}

		return out;
	}
	
	public void toLogin(){
		myView.startActivity(new Intent(myView, LoginView.class));
	}

}
